package com.yonyou.pmclouds.processphoto.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.pmpub.entity.QueryListParam;
import com.yonyou.pmclouds.pmpub.rmiitf.IPageQuerySerivce;
import com.yonyou.pmclouds.procedure.entity.ProcedureVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import com.yonyou.pmclouds.processphoto.entity.ProcessPhotoVO;
import com.yonyou.pmclouds.processphoto.mapper.ProcessPhotoMapper;
import com.yonyou.pmclouds.processphoto.rmiitf.ProcessPhotoQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProcessPhotoQuery.class)
public class ProcessPhotoQueryImpl implements ProcessPhotoQuery , IPageQuerySerivce<ProcessPhotoVO> {
    @Autowired
    private ProcessPhotoMapper mapper;
    @Autowired
    private PictureRemoteService picService;
    @Autowired
    private ProjectSimpleQuery projectQuery;
    @Autowired
    private ProcedureQuery processQuery;
    @Autowired
    private OrganizationQuery organizationQuery;

    @Override
    public ProcessPhotoVO[] queryByProject(String pk_project) throws BusinessException {
        ProcessPhotoVO[] vos = mapper.selectByPkProject(pk_project);
        if (ArrayUtils.isEmpty(vos)) {
            return null;
        }
        return setRefFiledInfo(vos);
    }

    @Override
    public ProcessPhotoVO queryOne(String pkProcessphoto) throws BusinessException {
        if (StringUtils.isEmpty(pkProcessphoto)) {
            return null;
        }
        ProcessPhotoVO vo = mapper.getById(pkProcessphoto);
        if (null == vo) {
            return null;
        }
        String pk_bill = vo.getPkProcessphoto();
        // 查询照片
        vo.setPictures(queryPics(pk_bill));
        return setRefFiledInfo(vo);
    }


    private String[] queryPics(String pk_bill) throws BusinessException {
        return picService.queryPicNamesByPkEntity(pk_bill);
    }

    /**
     * 处理参照字段，非必输项需校验空
     *
     * @param vo
     * @return
     * @throws BusinessException
     */
    private ProcessPhotoVO setRefFiledInfo(ProcessPhotoVO vo) throws BusinessException {
        if (vo == null) {
            return null;
        }
        // 工序
        String pk_process = vo.getPk_process();
        if (!StringUtils.isEmpty(pk_process)) {
            ProcedureVO process = processQuery.getById(pk_process);
            if (process != null) {
                vo.setProcessName(process.getSname());
            }
        }
        // 项目
        ProjectVO projectVO = projectQuery.queryProject(vo.getPk_project());
        vo.setProject_code(projectVO.getScode());
        vo.setProject_name(projectVO.getSname());
        vo.setManager(projectVO.getManager());

        String pkOrg = projectVO.getPkOrg();
        if (StringUtils.isNotEmpty(pkOrg)) {
            OrganizationVO organizationVO = organizationQuery.queryByPkSrc(RuntimeEnvironment.getTenantId(), pkOrg);
            vo.setOrgName(organizationVO == null ? null : organizationVO.getSname());
        }


        return vo;
    }

    @Override
    public boolean queryRefByIds(String code, String[] ids) throws BusinessException {
        boolean flag = false;
        Integer count = mapper.selectRefByCode(code, ids);
        if (count != null && count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public PageObject<Object> queryByCondition(String[] pkProjects, String[] creators, String startDate, String endDate, int[] billStatus, int start, int size) throws BusinessException {

        int count = mapper.countByCondition(pkProjects, creators, startDate, endDate, billStatus);

        ProcessPhotoVO[] processPhotoVOs = mapper.queryByCondition(pkProjects, creators, startDate, endDate, billStatus, start, size);

        return new PageObject<Object>(setRefFiledInfo(processPhotoVOs), count, start);
    }

    /**
     * 处理参照字段，非必输项需校验空
     *
     * @param vos
     * @return
     * @throws BusinessException
     */
    private ProcessPhotoVO[] setRefFiledInfo(ProcessPhotoVO[] vos) throws BusinessException {
        if (ArrayUtils.isEmpty(vos)) {
            return new ProcessPhotoVO[0];
        }

        Set<String> projectSet = new HashSet<String>();
        Set<String> pkSet = new HashSet<String>();
        Set<String> procSet = new HashSet<String>();

        for (ProcessPhotoVO vo :
                vos) {
            projectSet.add(vo.getPk_project());
            procSet.add(vo.getPk_process());
            pkSet.add(vo.getPkProcessphoto());
        }

        Map<String, ProjectVO> projectVOMap = projectQuery.queryProjects(projectSet.toArray(new String[0]));
        Map<String, List<String>> picMap = picService.queryPicsByPkEntitys(pkSet.toArray(new String[0]));
        Map<String, String> procedureNameMap = processQuery.queryNameByIds(procSet.toArray(new String[0]));


        for (ProcessPhotoVO vo :
                vos) {
            vo.setProject_name(projectVOMap.get(vo.getPk_project()) == null ? null : projectVOMap.get(vo.getPk_project()).getSname());
            vo.setProject_code(projectVOMap.get(vo.getPk_project()) == null ? null : projectVOMap.get(vo.getPk_project()).getScode());
            vo.setProcessName(procedureNameMap.get(vo.getPk_process()));
            List<String> pics = picMap.get(vo.getPkProcessphoto());
            if (pics != null) {
                vo.setPicNum(pics.size());
                vo.setPicturesCount(pics.size());
                vo.setPictures(pics.toArray(new String[0]));  //添加照片
            }
        }

        return vos;
    }

    @Override
    public int countByProject(QueryListParam param) throws BusinessException{
        int count = mapper.countByCondition( new String[]{param.getPkProject()}, null,  param.getStartDate(), param.getEndDate(), null);

        return count;
    }

    @Override
    public ProcessPhotoVO[] queryByProject(QueryListParam param) throws BusinessException {

        ProcessPhotoVO[] vos = mapper.queryByCondition(new String[]{param.getPkProject()}, null,  param.getStartDate(), param.getEndDate(), null,param.getPageStart(),param.getPageSize());

        return setRefFiledInfo(vos);
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PROCESSPHOTO_BILLTYPE;
    }
}
